import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//添加操作
public class AddBooks {
    public static void main(String[] args) throws SQLException {
        //1，获得数据源DataSource(设置Mysql的服务器地址)
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/book?characterEncoding=utf8&&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        //2.获得连接
        Connection connection= (Connection) dataSource.getConnection();
        //3.得到执行器
        String insertSQL="insert into books(name,author) values(?,?)";
        PreparedStatement statement=connection.prepareStatement(insertSQL);
        statement.setString(1,"水浒传");
        statement.setString(2,"施耐庵");
        //4.执行SQL
        int result=statement.executeUpdate();
        System.out.println("受影响的行数:"+result);
        //5.关闭资源
        statement.close();
        connection.close();
    }
}
